<?php
	/**
	 * Created by PhpStorm.
	 * User: mybook-lhp
	 * Date: 18/7/4
	 * Time: 下午3:27
	 */

	namespace app\payment\api;


	use app\common\controller\API;
	use app\payment\model\PostOrderModel;
	use app\payment\model\WalletModel;

	/**
	 * 提现(接入类)
	 * Class WithdrawOrder
	 * @package app\payment\api
	 */
	class WithdrawOrder extends API
	{
		/**
		 * 提现列表
		 * api POST /payment/PostOrder/index
		 * @param int member_id 用户ID
		 * @param int page 页码
		 * @return array data 返回值
		 */
		public function index()
		{
			$this->error('没有订单信息');
			$page = $this->request->param('page', '');
			if (empty(env('member_id')))
				$this->error('用户ID不存在！');
			$where['member_id'] = env('member_id');;
			$lists = PostOrderModel::getLists($where, $page);
			$countPage = PostOrderModel::countPage($where);
			$list = [];
			foreach ($lists as $x => $y)
			{
				$list[$x]['direction'] = $y['integrals'];
				$list[$x]['admin_status'] = $y['admin_status'];
				$list[$x]['time'] = date("Y-m-d H:i", $y['create_time']);
				if ($y['admin_status'] == 3)
					$list[$x]['time'] = date("Y-m-d H:i", $y['che_time']);

				$list[$x]['hand_money'] = $y['post_hand_money'];
				$list[$x]['money'] = $y['post_money'];
				$list[$x]['types'] = $y['post_type'];
				$list[$x]['order_sn'] = $y['order_sn'];
				$list[$x]['s_money'] = $y['actual_money'];
			}
			$data = ['page' => $countPage, 'data' => $list];
			if (empty($list))
				$this->error('没有订单信息');
			$this->result($data);
		}

		/**
		 * 提现提交
		 * api POST /payment/PostOrder/postmoney
		 * @param int member_id 用户ID
		 * @param char money 提现金额
		 * @param int post_type 提现方式
		 * @param char back_name 用户名字（持卡人姓名）
		 * @param char back_nubmer 提现银行卡号码
		 * @param char back_type 银行名字 例如：中国银行
		 * @param char ali_nubmer 支付宝账号
		 * @param char ali_name 用户名字（支付宝真实姓名）
		 * @return array data 返回值
		 */
		public function postmoney()
		{
			$post_type = $this->request->param('post_type', '');
			$money = $this->request->param('money', '');
			$back_type = $this->request->param('rank_name', '');
			$nubmer = $this->request->param('account', '');
			$name = $this->request->param('name', '');
			$data = PostOrderModel::postmsg(env('member_id'), $post_type, $money, $name, $nubmer, $back_type);
			if ($data['code'] == 0)
				$this->error($data['msg']);
			$this->result($data['msg']);
		}

		/**
		 * 撤销提现
		 * api POST /payment/PostOrder/postmoneyoff
		 * @param int member_id 用户ID
		 * @param int order_sn 订单号码
		 * @return array data 返回值
		 */
		public function postmoneyoff()
		{
			$this->error('订单号错误');///
			$order_sn = $this->request->param('order_sn', '');
			if (empty($order_sn))
				$this->error('订单号错误');
			$data = PostOrderModel::offorder(env('member_id'), $order_sn);
			if ($data['code'] == 0)
				$this->error($data['msg']);
			$this->result($data['msg']);
		}

		/**
		 * 提现计算
		 * api POST /payment/post_order/calculationmoney
		 * @return array data 返回值
		 */
		public function calculationmoney()
		{
			$this->error('余额不足');
			$money = $this->request->param('money', '');
			$user_money = WalletModel::userwallet(env('member_id'));
			if ($money > 0 || $money != 'undefined')
			{
				if ($user_money['reward_money'] < $money)
					$this->error('余额不足');
				$data = PostOrderModel::calculationmoney($money, 1);
			}
			$data['money'] = $user_money['reward_money'];
			$data['min_money'] = 100;
			$data['title'] = '提现金额（收取8%手续费，其中30%为手续费，70%为商城积分。最小提现金额100，且必须为100的倍数）';
			$this->result($data);
		}

	}